"use strict";

function init() {
    var vm = new Vue({
        el: "#app",
        data: {
            backUrl: '../login/login.html',
            formData: {
                name:'',
                password:'',
                status: 0
            },
            checkPassword: '',

            isActive: false,
            hideLoadingToast: true,
            errorAlert: {
                hideErrorAlert: true,
                errMsg : "未知错误"
            }
        },
        methods: {
            fieldChange() {
                if(this.formData.name && this.formData.password && this.checkPassword) {
                    this.isActive = true
                } else {
                    this.isActive = false
                }
            },
            validPassword() {
                return this.formData.password === this.checkPassword
            },
            submit() {
                if(!this.isActive)  return;
                if(!this.validPassword()) {
                    this.errorAlert.errMsg = "两次输入的密码不一样"
                    this.errorAlert.hideErrorAlert = false
                    return;
                }

                var _this = this;
                this.hideLoadingToast = false
                userService.add(this.formData, function(data) {
                    _this.hideLoadingToast = true
                    _this.success()
                }, function(errMsg) {
                    _this.hideLoadingToast = true
                    _this.errorAlert.errMsg = errMsg;
                    _this.errorAlert.hideErrorAlert = false
                });
            },
            success() {
                mm.go(this.backUrl)
            },
            hideAlert() {
                this.errorAlert.hideErrorAlert = true
            },
            back() {
                mm.go(this.backUrl)
            }
        },
        created() {
            // 获取从哪个页面跳转过来的页面，如果没有，则跳转到登录页面
            this.backUrl = mm.getUrlParam("fromUrl") || "../login/login.html";
            console.log(this.backUrl)
        },
    })
}

init();